//
//  JobsChooseViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/26.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "JobsChooseViewController.h"
#import "JobsChooseModel.h"

static NSString *setCell = @"setCell";
@interface JobsChooseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *buttonChoose;

@property (nonatomic,assign) NSInteger indexC;

@property(nonatomic, strong) NSMutableArray *bigCateData;
@property(nonatomic, strong) NSArray *smallCateData;

@property (weak, nonatomic) IBOutlet UIView *viewAddResumeShow;

@property (weak, nonatomic) IBOutlet UIView *viewScreeShow;

@property (nonatomic,assign) NSInteger isChoonse;

@end

@implementation JobsChooseViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = Color(1, 183, 287);
    TitleView(@"岗位筛选")
    _indexC = 0;
//    _bigCateData = @[].mutableCopy;
//    _smallCateData = @[].mutableCopy;
    [self getDataId];
    self.scrollView.hidden = NO;
    
    if (_addResume) {
        _viewAddResumeShow.hidden = 0;
    }
    
}

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 130, HEIGHT-50-64)];
        _scrollView.backgroundColor = Color(246, 246, 246);
        _scrollView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:_scrollView];
        
    }
    return _scrollView;
}

-(UIButton *)buttonChoose{
    if (!_buttonChoose) {
        
        for (int i=0; i<_bigCateData.count; i++) {
            JobsChooseModel *model = _bigCateData[i];
            _smallCateData =model.children;
             _buttonChoose = [UIButton buttonWithType:UIButtonTypeSystem];
            _buttonChoose.frame = CGRectMake(0, 50*i, 130, 50);
            [_buttonChoose setTitle:model.p_cate_name forState:UIControlStateNormal];
            [_buttonChoose setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            _buttonChoose.tag = 1000 + i;
            [_buttonChoose addTarget:self action:@selector(OnClickLister:) forControlEvents:UIControlEventTouchUpInside];
            [_scrollView addSubview:_buttonChoose];
            _scrollView.contentSize = CGSizeMake(130, 50*i+50);
            
            if (i==_indexC) {
                _buttonChoose.backgroundColor = [UIColor whiteColor];
                [_buttonChoose setTitleColor: Color(1, 183, 287) forState:UIControlStateNormal];
            }
           
        }
        
        
    }
    return _buttonChoose;
}

-(void)OnClickLister:(UIButton *)sender{
    NSInteger i = sender.tag - 1000;
    if (_indexC == i) {
        return;
    }
    
    UIButton *buttonNew = [self.view viewWithTag:1000+_indexC];
    [buttonNew setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    buttonNew.backgroundColor = [UIColor clearColor];
    sender.backgroundColor = [UIColor whiteColor];
    [sender setTitleColor:Color(1, 183, 287) forState:UIControlStateNormal];
    _indexC = i;
    
    JobsChooseModel *model = _bigCateData[sender.tag-1000];
    _smallCateData =model.children;
    
    [self.tableView reloadData];
    
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(130, 0, WIDTH-130, HEIGHT-50-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        _tableView.rowHeight = 50;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

#pragma mark  - 代理 -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.smallCateData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:setCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:setCell];
    }
    cell.textLabel.text = _smallCateData[indexPath.row][@"cate_name"];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.highlightedTextColor = Color(1, 183, 287);
    
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor whiteColor];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    _isChoonse = 1;
    
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"volue" object:nil userInfo:@{@"cate_name":_smallCateData[indexPath.row][@"cate_name"],
                   @"cate_id":_smallCateData[indexPath.row][@"id"]}];
    
}

- (IBAction)submit:(id)sender {
    if (_isChoonse != 1) {
        SHuiMBhudTextTimeStr(@"请选择岗位");
        return;
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (IBAction)limit:(id)sender {
    
    //发送通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"volue" object:nil userInfo:@{@"cate_name":@"不限",
                   @"cate_id":@""}];
    
     [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - 获取数据
- (void)getDataId{
    NSString *strUrl = [NSString stringWithFormat:post_Cate_api];

    [PostNetWorking postNetWorkingStr:strUrl andparameters:nil Success:^(id responseObject) {
        int code = [responseObject[@"code"]intValue];
        switch (code) {
            case 200:
            {
                self.bigCateData = [JobsChooseModel parsingDateFromResultDict:responseObject andData:nil];
                
                self.buttonChoose.hidden = NO;
                 self.tableView.hidden = NO;
                
            }
                break;
            case 210:
            {
                //逻辑错误
                [MBProgressHUD showMessag:responseObject[@"mark"] withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
                break;
            case 220:
            {
                //token过期
                [MBProgressHUD showMessag:@"登录状态已过期，请重新登录" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
            }
            default:
                break;
        }
        
    } failed:^(NSError *error) {
        [MBProgressHUD showMessag:@"网络错误" withDelay:0 toView:self.view withMode:MBProgressHUDModeText hideWithDelay:1.5];
    }];
}



@end
